﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Text.RegularExpressions;

namespace HubbleDataAccess
{
    /// <summary>
    /// 访问 Hubble 内置参数
    /// </summary>
    public class HubbleHelper
    {
        /// <summary>
        /// 获取字段过滤值，如"北京^5000^2",sqlclient
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="searchKeyWord">关键词</param>
        /// <param name="bySpace">用空格分割的关键词</param>
        /// <returns></returns>
        public static string GetMatchString(string tableName, string fieldName, string searchKeyWord,out string bySpace)
        {
            return HubbleDBAccess.GetKeywordAnalyzerString(tableName, fieldName, searchKeyWord,out bySpace);
        }

        /// <summary>
        /// 获取字段对应的分词器名称
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static AnalyzerName GetFieldAnalyzerName(string tableName ,string fieldName)
        {
            string analyzerSql = string.Format("exec SP_Columns '{0}'",tableName);
            DataTable table = HubbleDBAccess.ExecuteDataTable(analyzerSql);
            foreach (DataRow row in table.Rows)
            {
                if (row["FieldName"].ToString().Equals(fieldName, StringComparison.CurrentCultureIgnoreCase))
                {
                    switch(row["AnalyzerName"].ToString())
                    {
                        case "PanGuSegment":
                            return AnalyzerName.PanGuSegment;
                        case "EnglishAnalyzer":
                        default:
                            return AnalyzerName.EnglishAnalyzer;
                    }
                }
            }
            return AnalyzerName.EnglishAnalyzer;
        }

        /// <summary>
        /// 获取字段过滤值，如"北京^5000^2", 非 sqlclient
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="searchKeyword"></param>
        /// <returns></returns>
        public static string GetMatchStringSelf(string tableName,string fieldName,string searchKeyword)
        {
            string analyzerFmt = "exec SP_FieldAnalyze '{0}','{1}','{2}'";
            Regex reg = new Regex("[a-zA-Z0-9]");
            if(!reg.IsMatch(searchKeyword))
                analyzerFmt = "exec SP_FieldAnalyze '{0}','{1}','{2}', 'sqlclient'";
            string analyzerSql = string.Format(analyzerFmt,
                tableName.Replace("'", "''"), fieldName.Replace("'", "''"), searchKeyword.Replace("'", "''"));
            DataTable table = HubbleDBAccess.ExecuteDataTable(analyzerSql);
            StringBuilder resultMatch = new StringBuilder();
            foreach (DataRow row in table.Rows)
            {
                string word = row["Word"].ToString().Replace("'", "''");
                resultMatch.AppendFormat("{0}^{1}^{2} ", word, row["Rank"], row["Position"]);
            }
            return resultMatch.ToString().Trim();
        }
    }
}
